package com.hjj.springboot.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hjj.springboot.config.LogType;
import com.hjj.springboot.config.SystemLogs;
import com.hjj.springboot.entity.*;
import com.hjj.springboot.service.ITalkDetailService;
import com.hjj.springboot.service.ITalkImageService;
import com.hjj.springboot.service.ITalkService;
import com.hjj.springboot.service.IWxUserInfoService;
import com.hjj.springboot.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/talk")
public class AdminTalkController {

    @Autowired
    private ITalkService talkService;

    @Autowired
    private IWxUserInfoService wxUserInfoService;

    @Autowired
    private ITalkImageService talkImageService;

    @Autowired
    private ITalkDetailService talkDetailService;

    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean) {
        String query = pageBean.getQuery().trim();
        Page<Talk> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<Talk> pageResult = talkService.page(page, new QueryWrapper<Talk>().like(StringUtil.isNotEmpty(query),"content",query).orderByDesc("id"));
        for (Talk talk : pageResult.getRecords()) {
            talk.setWxUserInfo(wxUserInfoService.getOne(new QueryWrapper<WxUserInfo>().eq("openid", talk.getUid())));
            talk.setTalkImagesList(talkImageService.list(new QueryWrapper<TalkImage>().eq("mid", talk.getId())));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageResult.getTotal());
        map.put("talkList", pageResult.getRecords());
        return R.ok(map);
    }

    @SystemLogs(operation = "在线论坛",type = LogType.DELETE)
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        talkService.removeById(id);
        talkDetailService.remove(new QueryWrapper<TalkDetail>().eq("mid", id));
        talkImageService.remove(new QueryWrapper<TalkImage>().eq("mid", id));
        System.out.println(id);
        return R.ok();
    }

    @SystemLogs(operation = "在线论坛",type = LogType.BATCH_DELETE)
    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids) {
        talkService.removeByIds(ids);
        talkDetailService.remove(new QueryWrapper<TalkDetail>().in("mid", ids));
        talkImageService.remove(new QueryWrapper<TalkImage>().in("mid", ids));
        return R.ok();
    }

    @GetMapping("/export")
    public void export(@RequestParam(required = false) String query, HttpServletResponse response)   throws Exception {
        String queryStr=query.trim();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("论坛信息", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<Talk> talkList = talkService.list(new QueryWrapper<Talk>().like(StringUtil.isNotEmpty(queryStr),"content",query));
        EasyExcel.write(response.getOutputStream(),Talk.class)
                .sheet("论坛信息")
                .doWrite(talkList);
    }

}
